#!/bin/bash
db_host="127.0.0.1"
db_port=3306
db_user="root"
db_password="123456"

doSql () {
    mysql -h${db_host} -P${db_port} -u${db_user} -p"${db_password}" -e "$*"
}

sql="create database if not exists db_zwx2018 default charset=utf8 collate=utf8_general_ci;"
echo "create database ..."
doSql $sql
echo "ok"

echo "create table ..."

i=0
db="db_zwx2018"
while (($i <= 99))
do
    hashStr=`printf "%02d" ${i}`

    echo "create table t_user_info_${hashStr}"
    sql="create table ${db}.t_user_info_${hashStr} (
           \`uid\` int unsigned not null primary key,
           \`nick\` varchar(200) not null comment '昵称，base64编码',
           \`avatar\` varchar(255) not null default '' comment '头像',
           \`last_token\` varchar(64) not null default '' comment '上次登录的token',
           \`last_id_source\` tinyint unsigned not null default 0 comment '上次登录来源',
           \`bio\` varchar(255) not null default '' comment '个人简介',
           \`create_time\` int unsigned not null default 0 comment '创建时间',
           \`update_time\` int unsigned not null default 0 comment '更新时间'
         ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户信息';"
    doSql $sql

    echo "create table t_user_login_${hashStr}"
    sql="create table ${db}.t_user_login_${hashStr} (
           \`id\` varchar(255) not null comment '邮箱/手机/第三方平台id',
           \`id_source\` tinyint unsigned not null default 0 comment '0:邮箱注册,1:手机注册,2:微信,3:QQ',
           \`uid\` int unsigned not null comment '用户id',
           \`password\` varchar(64) null comment '登录密码',
           \`create_time\` int unsigned not null default 0,
           primary key (\`id\`, \`id_source\`)
         ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户登录信息';"
    doSql $sql

    echo "create table t_user_bind_${hashStr}"
    sql="create table ${db}.t_user_bind_${hashStr} (
           \`uid\` int unsigned not null comment '用户id',
           \`id\` varchar(255) not null comment '邮箱/手机/第三方平台id',
           \`id_source\` tinyint unsigned not null default 0 comment '0:邮箱注册,1:手机注册,2:微信,3:QQ',
           \`open_id\` varchar(255) not null default '' comment '微信/QQ unionid',
           \`create_time\` int unsigned not null default 0,
           primary key (\`uid\`, \`id\`, \`id_source\`)
         ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='用户绑定信息';"
    doSql $sql

    let "i++"
done

